package com.lt.flashcard.model.entity;

import java.util.Date;

import com.google.code.morphia.annotations.Embedded;

/**
 * The response from webservice
 * @author ocsen
 *
 */
@Embedded
public class PhoneCardResponse {

	private String error_code = "";
	private String merchant_id = "";
	private String merchant_account = "";
	private String pin_card = "";
	private String card_serial = "";
	private String type_card = "";
	private String ref_code = "";
	private String client_fullname = "";
	private String client_email = "";
	private String client_mobile = "";
	private int card_amount = 0;
	private int transaction_amount = 0;
	private String transaction_id = "";
	
	private String data = "";
	
	private Date created = new Date();
	
	public PhoneCardResponse(String data) {
		String[] pairs = data.split("\\|");
		this.data = data;
		if(data.replaceAll("[^\\|]", "").length() != 12) {
			this.error_code = "99";
		}
		
		int size = pairs.length;
		
		setError_code(pairs[0]);
		setMerchant_id(pairs[1]);
		setMerchant_account(pairs[2]);
		setPin_card(pairs[3]);
		setCard_serial(pairs[4]);
		setType_card(pairs[5]);
		if(size > 6)
			setRef_code(pairs[6]);
		if(size > 7)
			setClient_fullname(pairs[7]);
		if(size > 8)
			setClient_email(pairs[8]);
		if(size > 9)
			setClient_mobile(pairs[9]);
		if(size > 10)
			setCard_amount(Integer.parseInt(pairs[10]));
		if(size > 11)
			setTransaction_amount(Integer.parseInt(pairs[11]));
		if(size > 12)
			setTransaction_id(pairs[12]);
	}
	
	@Override
	public String toString() {
		return String.format("CardRespond<Errcode: %s, mid: %s, macc: %s, pin: %s, serrial: %s, type: %s, ref: %s, name: %s, email: %s, mobile: %s, amount: %s, trans_amount: %s, trans_id: %s>",
				getError_code(), getMerchant_id(), getMerchant_account(), getPin_card(), getCard_serial(), getType_card(), getRef_code(), getClient_fullname(), getClient_email(), getClient_mobile(), getCard_amount(), getTransaction_amount(), getTransaction_id());
	}
	
	public String getError_code() {
		return error_code;
	}
	
	public void setError_code(String error_code) {
		this.error_code = error_code;
	}
	
	public String getMerchant_id() {
		return merchant_id;
	}
	
	public void setMerchant_id(String merchant_id) {
		this.merchant_id = merchant_id;
	}
	
	public String getMerchant_account() {
		return merchant_account;
	}
	
	public void setMerchant_account(String merchant_account) {
		this.merchant_account = merchant_account;
	}
	
	public String getPin_card() {
		return pin_card;
	}
	
	public void setPin_card(String pin_card) {
		this.pin_card = pin_card;
	}
	
	public String getCard_serial() {
		return card_serial;
	}
	
	public void setCard_serial(String card_serial) {
		this.card_serial = card_serial;
	}
	
	public String getType_card() {
		return type_card;
	}
	
	public void setType_card(String type_card) {
		this.type_card = type_card;
	}
	
	public String getRef_code() {
		return ref_code;
	}
	
	public void setRef_code(String ref_code) {
		this.ref_code = ref_code;
	}
	
	public String getClient_fullname() {
		return client_fullname;
	}
	
	public void setClient_fullname(String client_fullname) {
		this.client_fullname = client_fullname;
	}
	
	public String getClient_email() {
		return client_email;
	}
	
	public void setClient_email(String client_email) {
		this.client_email = client_email;
	}
	
	public String getClient_mobile() {
		return client_mobile;
	}
	
	public void setClient_mobile(String client_mobile) {
		this.client_mobile = client_mobile;
	}
	
	public int getCard_amount() {
		return card_amount;
	}
	
	public void setCard_amount(int card_amount) {
		this.card_amount = card_amount;
	}
	
	public int getTransaction_amount() {
		return transaction_amount;
	}
	
	public void setTransaction_amount(int transaction_amount) {
		this.transaction_amount = transaction_amount;
	}
	
	public String getTransaction_id() {
		return transaction_id;
	}
	
	public void setTransaction_id(String transaction_id) {
		this.transaction_id = transaction_id;
	}
	
	public String getData() {
		return data;
	}
	
	public boolean isSuccess() {
		return getError_code().equals("00");
	}
	
	public String getMessage() {
		String result = "";
		if(error_code.equals("00"))
			result = "Thành công"; //Success
		else if(error_code.equals("99"))
			result = "Lỗi mạng"; //Unknow
		else if(error_code.equals("01"))
			result = "IP bị khóa"; //IP denied
		else if(error_code.equals("02"))
			result = "Thông số card bị lỗi"; //Param error
		else if(error_code.equals("03"))
			result = "Lỗi mạng"; //Merchant blocked or unknow
		else if(error_code.equals("04"))
			result = "Mã kiểm tra sai"; //Wrong checksum
		else if(error_code.equals("05"))
			result = "Tài khoản không biết"; //Unknow account
		else if(error_code.equals("06"))
			result = "Tài khoản bị khóa"; //Account blocked
		else if(error_code.equals("07"))
			result = "Card đã sử dụng"; //Used card
		else if(error_code.equals("08"))
			result = "Card bị khóa"; //Blocked card
		else if(error_code.equals("09"))
			result = "Card hết hạn sử dụng"; //Expired card
		else if(error_code.equals("10"))
			result = "Card không tồn tại"; //Card unknow
		else if(error_code.equals("11"))
			result = "Card không hợp lệ"; //Wrong card format
		else if(error_code.equals("12"))
			result = "Sai số serial"; //Wrong seri
		else if(error_code.equals("13"))
			result = "Mã thẻ hoặc số serial không đúng"; //Wrong card number or seri
		else if(error_code.equals("14"))
			result = "Card không tồn tại"; //Card unknow
		else if(error_code.equals("15"))
			result = "Card không sử dụng được"; //Unsabled card
		else if(error_code.equals("16"))
			result = "Sai nhiều quá"; //Wrong too much
		else if(error_code.equals("17"))
			result = "Mạng có lỗi"; //System error
		else if(error_code.equals("18"))
			result = "Mạng có lỗi"; //System error
		else if(error_code.equals("19"))
			result = "Mạng có lỗi"; //System error
		else if(error_code.equals("20"))
			result = "Mạng có lỗi";
		return result;
	}

	/**
	 * @return the created
	 */
	public Date getCreated() {
		return created;
	}

	/**
	 * @param created the created to set
	 */
	public void setCreated(Date created) {
		this.created = created;
	}
}
